package com.llxy.worker.service.impl;

import com.llxy.common.utils.SecurityUtils;
import com.llxy.worker.domain.Homemaking;
import com.llxy.worker.dto.HomemakingRespDTO;
import com.llxy.worker.dto.SupportJobDTO;
import com.llxy.worker.mapper.HomemakingJobMapper;
import com.llxy.worker.mapper.HomemakingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.llxy.worker.mapper.SupportJobMapper;
import com.llxy.worker.domain.SupportJob;
import com.llxy.worker.service.SupportJobService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupportJobServiceImpl implements SupportJobService{

    @Resource
    private SupportJobMapper supportJobMapper;
    @Autowired
    private HomemakingJobMapper homemakingJobMapper;
    @Autowired
    private HomemakingMapper homemakingMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return supportJobMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SupportJob record) {
        return supportJobMapper.insert(record);
    }

    @Override
    public int insertSelective(SupportJob record) {
        return supportJobMapper.insertSelective(record);
    }

    @Override
    public SupportJob selectByPrimaryKey(Integer id) {
        return supportJobMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SupportJob record) {
        return supportJobMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SupportJob record) {
        return supportJobMapper.updateByPrimaryKey(record);
    }

    @Override
    public Integer selectBySupportIdCount(Long userId) {
        return supportJobMapper.selectBySupportIdCount(userId);
    }

    @Override
    public int deliver(Integer jobId,Integer supportId) {
        SupportJob supportJob = new SupportJob();
        supportJob.setStatus(0);
        supportJob.setSupportWorkerId(supportId);
        supportJob.setJobId(jobId);
        Integer homemakingId = homemakingJobMapper.selectByPrimaryKey(jobId).getHomemakingId();
        Homemaking homemaking = homemakingMapper.selectByPrimaryKey(homemakingId);
        Long homemakingUserId = homemaking.getUserId();
        supportJob.setHomemakingUserId(homemakingUserId);
        return supportJobMapper.insertSelective(supportJob);
    }

    @Override
    public List<SupportJobDTO> deliverHistory(Long userId) {
        List<SupportJob> lists = supportJobMapper.deliverHistory(userId);
        List<Integer> jobIds = lists.stream().map(SupportJob::getJobId).collect(Collectors.toList());
        if (null != jobIds && jobIds.size() > 0){
            return homemakingJobMapper.selectUserJobs(userId,jobIds);
        }
        return new ArrayList<>();
    }

    @Override
    public int selectCount(Long userId) {
        return supportJobMapper.selectCount(userId);
    }

    @Override
    public int selectJobCount() {
        return supportJobMapper.selectJobCount();
    }

    @Override
    public List<HomemakingRespDTO> selectList(Long homemakingUserId) {
        if (SecurityUtils.isAdmin(homemakingUserId)){
            return supportJobMapper.selectListByHomemakingUserId(null);
        }
        return supportJobMapper.selectListByHomemakingUserId(homemakingUserId);
    }

    @Override
    public int examine(Integer status,Integer id) {
        return supportJobMapper.examine(status,id);
    }


}
